﻿/*!
 *      Colour.cs         
 *      Date: 27/01/2012
 *      
 *      ----------------------------------------------------------------------
 *      A static wrapper for transparent use of HSL colour space in XNA 4.0
 *      ----------------------------------------------------------------------
 * 
 *      Usage:
 *      To Get an HSL colour as an RGB colour:
 *          Call: Colour.HSL(myHue, mySat, myLum);      (returns RGB colour)

 * 
 *      Author:
 *      Adrian Seeley adrian@gatosomina.com
 *      
 *      License:
 *      FULLY OPEN
 */


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace XNAREUSABLES
{
    /// <summary>
    /// Acts as a transparent wrapper for HSL colours in RGB space.
    /// </summary>
    public static class Colour
    {
        /// <summary>
        /// Returns an XNA RGB Color from HSL colour space.
        /// </summary>
        /// <param name="hue">The hue of the colour.</param>
        /// <param name="hue">The saturation of the colour.</param>
        /// <param name="hue">The luminescence of the colour.</param>
        /// <returns>Returns an XNA RGB color.</returns>
        public static Color HSL(double hue, double sat, double lum)
        {
            double v;
            double r = lum, g = lum, b = lum;     //default to gray

            v = (lum <= 0.5) ? (lum * (1.0 + sat)) : (lum + sat - lum * sat);

            if (v > 0)
            {
                double m;
                double sv;
                int sextant;
                double fract, vsf, mid1, mid2;

                m = lum + lum - v;
                sv = (v - m) / v;
                hue *= 6.0;
                sextant = (int)hue;
                fract = hue - sextant;
                vsf = v * sv * fract;
                mid1 = m + vsf;
                mid2 = v - vsf;
                switch (sextant)
                {
                    case 0:
                        r = v;
                        g = mid1;
                        b = m;
                        break;

                    case 1:
                        r = mid2;
                        g = v;
                        b = m;
                        break;

                    case 2:
                        r = m;
                        g = v;
                        b = mid1;
                        break;

                    case 3:
                        r = m;
                        g = mid2;
                        b = v;
                        break;

                    case 4:
                        r = mid1;
                        g = m;
                        b = v;
                        break;

                    case 5:
                        r = v;
                        g = m;
                        b = mid2;
                        break;
                }
            }

            return new Color((float)r, (float)g, (float)b); //return colour
        }

        /// <summary>
        /// Returns a random colour.
        /// </summary>
        /// <returns></returns>
        public static Color Random()
        {
            return HSL(Rando.Get.NextDouble(), Rando.Get.NextDouble(), Rando.Get.NextDouble());
        }

    }   // end class
}   // end namespace 
